Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Telephony

Namespace C4F.DevKit.Telephony.Controls
	Public Partial Class DialPanel
		Inherits UserControl
		#Region "Members"

		Private _tapiManager As TapiManager

		#End Region

		#Region "Properties"

		Public Property TapiManager() As TapiManager
			Get
				Return _tapiManager
			End Get

			Set(ByVal value As TapiManager)
				_tapiManager = value

				If value Is Nothing Then
					Return
				End If

				Try
					Dim lines As List(Of Line) = _tapiManager.GetAvailableLines(True)
					For Each line As Line In lines
						cmbAvailableLines.Items.Add(line.LineName)
					Next line

					cmbAvailableLines.SelectedIndex = 0
				Catch ex As Exception
					MessageBox.Show(ex.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End Set
		End Property

		<Browsable(True)> _
		Public Property DialOptionsButtonsEnabled() As Boolean
			Get
				Return dialPad1.Enabled
			End Get

			Set(ByVal value As Boolean)
				dialPad1.Enabled = value
				txtPhoneNumber.Enabled = value
				cmbAvailableLines.Enabled = value
				btnDisconnect.Enabled = Not value
				If txtPhoneNumber.Text <> String.Empty Then
					btnDial.Enabled = value
				Else
					btnDial.Enabled = Not value
				End If
			End Set
		End Property

		<Browsable(True)> _
		Public Property HoldButtonEnabled() As Boolean
			Get
				Return btnHold.Enabled
			End Get

			Set(ByVal value As Boolean)
				btnHold.Enabled = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property ResumeButtonEnabled() As Boolean
			Get
				Return btnResume.Enabled
			End Get

			Set(ByVal value As Boolean)
				btnResume.Enabled = value
			End Set
		End Property

		#End Region

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			AddHandler btnDial.Click, AddressOf btnDial_Click
			AddHandler btnHold.Click, AddressOf btnHold_Click
			AddHandler btnResume.Click, AddressOf btnResume_Click
			AddHandler btnDisconnect.Click, AddressOf btnDisconnect_Click
			AddHandler txtPhoneNumber.TextChanged, AddressOf txtPhoneNumber_TextChanged
			AddHandler dialPad1.DigitClicked, AddressOf dialPad1_DigitClicked
			AddHandler dialPad1.ClearButtonClicked, AddressOf dialPad1_ClearButtonClicked
			AddHandler dialPad1.BackButtonClicked, AddressOf dialPad1_BackButtonClicked
		End Sub

		#End Region

		#Region "Event Handlers"

		Private Sub btnDial_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				Dim targetAddress As String = txtPhoneNumber.Text
				Dim lineName As String = cmbAvailableLines.SelectedItem.ToString()
				_tapiManager.MakeCall(targetAddress, lineName)
			Catch ex As ArgumentNullException
				MessageBox.Show("Phone number is not provided.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Dim exception As String = ex.ToString()
			Catch ex As ArgumentException
				MessageBox.Show("Phone number is not valid.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Dim exception As String = ex.ToString()
			Catch ex As TapiManagerException
				MessageBox.Show("Error! Dialing can not be performed.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Dim exception As String = ex.ToString()
			Catch ex As Exception
				MessageBox.Show(ex.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnDisconnect_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				_tapiManager.DisconnectCall()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnHold_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHold.Click
			Try
				_tapiManager.Hold()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnResume_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				_tapiManager.Resume()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub txtPhoneNumber_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
			btnDial.Enabled = txtPhoneNumber.Text <> String.Empty
		End Sub

		Private Sub dialPad1_DigitClicked(ByVal digitChar As Char)
			txtPhoneNumber.Text += digitChar
		End Sub

		Private Sub dialPad1_ClearButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
			txtPhoneNumber.Clear()
		End Sub

		Private Sub dialPad1_BackButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
			Dim phoneNumber As String = txtPhoneNumber.Text

			If phoneNumber.Length > 0 Then
				txtPhoneNumber.Text = phoneNumber.Remove(phoneNumber.Length - 1)
			End If
		End Sub

		#End Region
	End Class
End Namespace
